<?php
if (!defined('IN_WONDERSHARE'))
{
	die('Hacking attempt');
}
require('./classes/module/module_bar.php');
class barControl
{
	var $bar_m;
	function __construct()
	{
		$this->bar_m = new barModule();
	}
	/**
	* 根据id得到bar信息
	*
	* @param   int         $bar_id
	*
	* @return  array
	*/
	function  get_bar_id_info($block_id)
	{
		$sql = "SELECT * FROM `block` WHERE `block_id` = '$block_id' AND `block_delete`=0";
		return $this->bar_m->get_bar_info($sql);
	}
	/**
	* 增加或者修改bar
	*
	* @param   string      $type            操作类型
	* @param   int         $bar_id          ban id
	* @param   string      $title     	     留言标题
	* @param   text        $content         留言内容
	* @return  int
	*/
	function  operate_bar($type,$block_id,$block_name,$block_title,$block_pic_url,$block_pic,$block_more,$block_content,$webType = 1)
	{
		if($type == 'add')
		{
			$sql ="
			INSERT INTO `block` (`block_name`,`block_title`,`block_pic_url`,`block_pic`,`block_content`,
			`block_add_user`,`block_update_user`,`block_add_time`,`block_update_time`,web_type) VALUES ('$block_name',
			'$block_title','$block_pic_url','$block_pic','$block_content','".$_SESSION['AName']."','".$_SESSION['AName']."',
			UNIX_TIMESTAMP(),UNIX_TIMESTAMP(),$webType) ";
		}
		elseif($type == 'edit')
		{
			$sql = "UPDATE `block` SET `block_name` = '$block_name',`block_title` = '$block_title',
			`block_pic_url` = '$block_pic_url',`block_pic` = '$block_pic',`block_content` = '$block_content',
			`block_update_user` = '".$_SESSION['AName']."',modify_status  = 1,`block_update_time` = UNIX_TIMESTAMP()
			WHERE `block_id` = '$block_id' ";
		}
		return $this->bar_m->change_info($sql);	 	
	}
	/**
	* 批量删除标签
	*
	* @param   string         $list_id    id字符串
	*
	* @return  int
	*/
	function delete_bar($list_id)
	{
		$sql = "UPDATE `block` SET `block_delete`=1 WHERE `block_id` IN ($list_id) ";
		return $this->bar_m->change_info($sql);
	}

	/**
	* 得到分页相关信息
	*
	* @param   int        $page       页码
	* @param   int        $type       为1代表没有条件查询 为2代表有条件查询
	* @param   int        $block_name  模块名称
	* @param   string     $block_title  模块显示标题
	* @return  array
	*/
	function get_url_info($page,$type,$webType = 1 ,$block_name = '',$block_title = '')
	{
		if($type == 'list')
		{
			$sql_p = 'SELECT COUNT(*) AS total FROM `block` WHERE web_type = '.$webType.'  AND block_delete  = 0  ';
		}
		elseif($type == 'list_s')
		{
			$where_str = ' web_type = '.$webType;
			$url_str = '&url_page=1';
			if(!empty($block_name))
			{
				$where_str.=' AND block_name LIKE "%'.$block_name.'%" ';
				$block_name =  urlencode(str_replace('\\','',$block_name));
				$url_str .= '&block_name='.$block_name;
			}
			if(!empty($block_title))
			{
				$url_str .= '&block_title='.urlencode(stripslashes($block_title));
				$where_str.=' AND block_title LIKE "%'.$block_title.'%" ';
			}
			$sql_p = 'SELECT COUNT(*) AS total FROM block WHERE '.$where_str.' AND block_delete  = 0    ';
		}
		$page_info = get_page_info($sql_p,15,$page);
		$page_url = get_page_url($page,$page_info['page_num'],$url_str);
		return  array('page_info'=>$page_info,'page_url'=>$page_url);
	}
	/**
	* 得到列表bar信息
	*
	* @param   string     $type       类型（list为列表 不带搜索 而 list_s 在列表 带有搜索）
	* @param   int        $start      从那行开始
	* @param   int        $pagesize   取多少条记录
	* @param   int        $block_name  模块名称
	* @param   string     $block_title  模块显示标题
	* @return  array
	*/
	function  get_bar_info($type,$start = 0,$pagesize = 10,$webType = 1,$block_name = '',$block_title = '')
	{
		if($type == 'list')
		{
			$sql = 'SELECT block_id , block_name , block_title , block_content , block_pic_url , block_pic ,
			block_update_user , block_add_time ,IF(block_update_time > 0, FROM_UNIXTIME(block_update_time, "%Y-%m-%d %H:%i"), "") updatetime
			FROM block	WHERE web_type = '.$webType.' AND block_delete = 0 ORDER BY block_id DESC LIMIT '.$start.','.$pagesize ;
		}
		elseif($type == 'list_s')
		{
			$where_str = '   web_type = '.$webType;
			if(!empty($block_name))
			{
				$where_str.=" AND `block_name` LIKE '%".$block_name."%' ";
			}
			if(!empty($block_title))
			{
				$where_str.=" AND `block_title` LIKE '%".$block_title."%' ";
			}
			
			$sql = '
			SELECT block_id , block_name , block_title , block_content , block_pic_url , block_pic ,
			block_update_user , block_add_time ,IF(block_update_time > 0, FROM_UNIXTIME(block_update_time, 
			"%Y-%m-%d %H:%i"), "") updatetime	FROM block	WHERE '.$where_str.' 
			ORDER BY block_id DESC LIMIT '.$start.','.$pagesize ;
		}
		return $this->bar_m->get_bar_info($sql);
	}

}
?>